//
//  THJCompanyListVC.m
//  THJXHW
//
//  Created by 河南德艺 on 2017/11/15.
//  Copyright © 2017年 河南德艺. All rights reserved.
//

#import "THJCompanyListVC.h"
#import "JXSegment.h"
#import "JXPageView.h"
#import "THJCompanyListVC.h"
#import "THJCompanyListCell.h"
#import "ZBNetworking.h"
#import "THJSelectView.h"
#import <MJRefresh.h>
#import "THJCompanyVC.h"
@interface THJCompanyListVC ()<JXSegmentDelegate,UITableViewDelegate,UITableViewDataSource,THJSelectViewDelegate>
{
    JXSegment * segment;
    UITableView * _tableview;
    NSInteger page;
    NSString * areaId;
    NSString * catid;
}
@property (weak, nonatomic) IBOutlet UIView *btnBgView;
@property (strong, nonatomic)  NSMutableArray *listArr;
@property (strong,nonatomic) NSMutableArray * titleArr;
@property (strong,nonatomic) NSMutableArray * titleIdArr;
@property (strong,nonatomic) NSMutableArray * areaArr;
@property (strong,nonatomic) THJSelectView * selectView;
@end

@implementation THJCompanyListVC
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = YES;
    [self setStateBarColor:MainColor];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setShadow];
    page = 1;
    areaId = @"0";
    [self getAreaData];
    [self getData];
    if(KIsiPhoneX){
        [self setXNavBarWithTitle:@"公司列表"];
    }else{
         [self setNavBarWithTitle:@"公司列表"];
    }
    self.topSpace.constant = NavbarHeight+45;
    // Do any additional setup after loading the view from its nib.
}
-(NSMutableArray*)listArr{
    if(!_listArr){
        _listArr = [[NSMutableArray alloc] init];
    }
    return _listArr;
}
-(NSMutableArray*)titleIdArr{
    if(!_titleIdArr){
        _titleIdArr = [[NSMutableArray alloc] init];
    }
    return _titleIdArr;
}
-(THJSelectView*)selectView{
    if(!_selectView){
        _selectView = [[THJSelectView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        _selectView.dataArr = self.areaArr;
        _selectView.delegate = self;
        _selectView.listKey = @"areaname";
    }
    return _selectView;
}
//获取滚动顶部菜单
-(void)getData{
    NSString * url = [NSString stringWithFormat:@"%@%@",THJUrlDomain,@"index.php?f=category&r=category&m=top&mid=4"];
    __weak typeof(self) weakSelf = self;
    [ZBRequestManager requestWithConfig:^(ZBURLRequest *request){
        request.urlString = url;
        request.apiType = ZBRequestTypeCache;
    }  success:^(id responseObject,apiType type){
        NSDictionary * receiveData = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"top-%@",receiveData);
        weakSelf.titleArr = receiveData[@"data"];
        NSMutableArray * arr = [NSMutableArray array];
        for (NSDictionary * dic in receiveData[@"data"]) {
            [arr addObject:dic[@"catname"]];
            [weakSelf.titleIdArr addObject:dic[@"catid"]];
        }
        catid = weakSelf.titleIdArr[0];
        [self setTitleMenuWithTitleArr:arr];
        [self getDataWithType:catid PageNo:1 Area:@"0"];
    } failed:^(NSError *error){
        
    }];
}
//获取地区数据
-(void)getAreaData{
    NSString * url = [NSString stringWithFormat:@"%@%@%@",THJUrlDomain,THJindexPHP,@"f=Area&r=Area"];
    __weak typeof(self) weakSelf = self;
    [THJNetWork GetDataWithGetRequestTypeUrl:url Callback:^(NSDictionary* receiveData, NSError *error) {
        NSLog(@"rece-%@",receiveData);
        weakSelf.areaArr = [receiveData[@"data"] mutableCopy];
        NSDictionary * dic = @{@"areaid":@"0",@"areaname":@"全国"};
        [weakSelf.areaArr insertObject:dic atIndex:0];
    }];
}
-(void)getDataWithType:(NSString*)type PageNo:(NSInteger)page Area:(NSString*)area{
    [MBProgressHUD showMessage:@"" toView:self.view];
    __weak typeof(self) weakself = self;
     NSString * url = [NSString stringWithFormat:@"%@index.php?f=company&r=company&page=%ld&areaid=%@&catid=%@",THJUrlDomain,page,area,type];
    NSLog(@"公司列表-%@",url);
    [ZBRequestManager requestWithConfig:^(ZBURLRequest *request){
        request.urlString = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        request.apiType=ZBRequestTypeCache;
    }  success:^(id responseObject,apiType type){
        NSDictionary * receiveData = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        [_tableview.mj_footer endRefreshing];
        [MBProgressHUD hideHUDForView:weakself.view animated:YES];
//        if([receiveData[@"status"] isEqual:@"200"]){
        
            for(NSDictionary * dic in receiveData[@"data"]){
                [weakself.listArr addObject:dic];
            }
        if([receiveData[@"nexpage"] isEqual:@"end"]){
            [_tableview.mj_footer endRefreshingWithNoMoreData];
        }
            dispatch_async(dispatch_get_main_queue(), ^{
               
             _tableview = [self setTableviewWithFrame:CGRectMake(0, NavbarHeight+45+40, ScreenWidth, ScreenHeight - NavbarHeight - 45 - 40) RegisterNibNameArr:@[@"THJCompanyListCell"] ReuseIdentifierArr:@[@"list"] RowHeight:110 BgColor:SingleColor(whiteColor) IsLoadMore:YES];
            });
//        }
    } failed:^(NSError *error){
        
    }];
}
-(void)loadMore{
    page++;
    [self getDataWithType:catid PageNo:page Area:areaId];
    [_tableview.mj_footer endRefreshing];
    NSLog(@"下拉加载");
}
-(void)setTitleMenuWithTitleArr:(NSArray*)arr{
    segment = [[JXSegment alloc] initWithFrame:CGRectMake(0, NavbarHeight-5, ScreenWidth, 45)];
    segment.scrollView.backgroundColor = COLOR(83, 84, 85, 1);
    [segment updateChannels:arr];
    segment.delegate = self;
    [self.view addSubview:segment];
}
-(void)setShadow{
    self.btnBgView.layer.shadowColor = [UIColor lightGrayColor].CGColor;//设置阴影的颜色
    self.btnBgView.layer.shadowOpacity = 0.8;//设置阴影的透明度
    self.btnBgView.layer.shadowOffset = CGSizeMake(5, 5);//设置阴影的偏移量
    self.btnBgView.layer.shadowRadius = 3;//设置阴影的圆角
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.listArr.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary * dic = self.listArr[indexPath.row];
    THJCompanyListCell * cell = [tableView dequeueReusableCellWithIdentifier:@"list" forIndexPath:indexPath];
    cell.companyLab.text = dic[@"company"];
    cell.addressLab.text = dic[@"address"];
    cell.zhuyingLab.text = dic[@"business"];
    [cell.logoImgV sd_setImageWithURL:[NSURL URLWithString:dic[@"logo"]] placeholderImage:kGetImage(@"占位图")];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary * dic = self.listArr[indexPath.row];
    THJCompanyVC * vc = [THJCompanyVC new];
    vc.cid = dic[@"cid"];
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - JXPageViewDataSource
-(NSInteger)numberOfItemInJXPageView:(JXPageView *)pageView{
    return 7;
}
-(UIView*)pageView:(JXPageView *)pageView viewAtIndex:(NSInteger)index{
    UIView *view = [[UIView alloc] init];
    return view;
}
#pragma mark - JXSegmentDelegate
-(void)JXSegment:(JXSegment*)segment didSelectIndex:(NSInteger)index{
    NSLog(@"index--%ld",index);
    [self.listArr removeAllObjects];
    catid = self.titleIdArr[index];
    [self getDataWithType:catid PageNo:1 Area:areaId];
}
#pragma mark - JXPageViewDelegate -
- (void)didScrollToIndex:(NSInteger)index{
    [segment didChengeToIndex:index];
    NSLog(@"**-%ld",(long)index);
}
#pragma mark --地区选择--
-(void)selectIndex:(NSInteger)index{
    
    [self setStateBarColor:MainColor];
    if(index != 999999){
        NSDictionary * dic = self.areaArr[index];
        [self.listArr removeAllObjects];
        [self.btn2 setTitle:dic[@"areaname"] forState:UIControlStateNormal];
        areaId = dic[@"areaid"];
        [self getDataWithType:catid PageNo:1 Area:areaId];
    }
    
}
- (IBAction)backBtnAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)btn1Action:(id)sender {
}
- (IBAction)btn2Action:(id)sender {
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    [self setStateBarColor:SingleColor(whiteColor)];
    [window addSubview:self.selectView];
}

@end
